package com.mmp.main.servlet.report;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.entity.JobOrder;
import com.mmp.main.util.Default;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

public class ViewDeviceHistory extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int siteId = getSessionSiteId(request);
		int locationId = getSessionLocationId(request);
		String code = WebUtil.getParamValue(request, "code", null);
		String sn = WebUtil.getParamValue(request, "sn", null);
		float prevCost = WebUtil.getParamValueAsFloat(request, "prevCost", 0f);

		/*
		 * HospitalDevice device = new HospitalDevice(); device.setCode(code);
		 * device.setSerialNo(sn);
		 * 
		 * JobOrder jo = new JobOrder(); jo.setHospitalDevice(device);
		 * 
		 * List<JobOrder> jOrders = dbm.find(jo);
		 * 
		 * if (jOrders == null || jOrders.isEmpty()) {
		 * announceErorrMessage(request, response,
		 * "لا يوجد أوامر عمل للجهاز المطلوب"); return; } else {
		 */

		HospitalDevice device = dbQueryManager.getDevice(siteId, locationId, code, sn, null, false, false);
		if (device == null) {
			announceErorrMessage(request, response, "لم يتم العثور علي أي نتائج موافقة لبحثك");
			return;
		}

		List<JobOrder> list = dbQueryManager.getDeviceJobOrders(siteId, locationId, code, sn);
		// if (list == null || list.isEmpty()) {
		// announceErorrMessage(request, response,
		// "لا توجد نتائج موافقة لبحثك");
		// return;
		// }

		HashMap params = new HashMap();
		// HospitalDevice device = list.get(0).getHospitalDevice();
		params.put(Default.REPORT_TITLE_PARAM_NAME, Enums.REPORT_TITLE.DEVICE_HISTORY.getReportTitle());
		params.put("jOrderList", list);
		params.put("device", device);
		params.put("prevCost", prevCost);
		params.put("deviceStatus", WebUtil.getDeviceStatusName(device));
		// params.put(JRParameter.REPORT_LOCALE, new Locale("ar"));
		viewPDFReport("rt4.jasper", null, params, request, response);

	}
}
